import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/net/post_request.dart';
import 'package:ls_work/net/response/response_data.dart';
import 'package:ls_work/net/response/service_order_ddetail_data.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/ui/service_order/continue_service_order_widget.dart';
import 'package:ls_work/ui/service_order/finish_service_order_widget.dart';
import 'package:ls_work/ui/service_order/receive_service_order_widget.dart';
import 'package:ls_work/ui/service_order/refuse_order_widget.dart';
import 'package:ls_work/ui/service_order/service_order_detail_info_widget.dart';
import 'package:ls_work/ui/service_order/service_order_process_widget.dart';
import 'package:ls_work/ui/service_order/start_service_order_widget.dart';
import 'package:ls_work/viewmodel/service_order/service_order_detail_info_view_model.dart';
import 'package:ls_work/widgets/my_sliver_delegate.dart';
import 'package:flutter/material.dart' as W;

import '../../ui/send_order/office_refuse_order_fragment.dart';
import '../../ui/send_order/send_order_fragment.dart';

class ServiceOrderDetailViewModel extends BaseViewModel {
  final TickerProvider provider;
  final String handleStatus;
  var showDetail = false;
  var orderId = "";

  static ServiceOrderDdetailData?  detailData;

  ServiceOrderDetailViewModel(
      BuildContext context, this.provider, this.showDetail, this.orderId,
      {this.handleStatus = ""})
      : super(context) {
    if (!handleStatus.isNullOrEmpty()) {
      current = handleStatus;
      if (current == '待完成'||current=='待派单'||current == '待开始') {
        controller = TabController(length: titles.length, vsync: this.provider);
      }
    }
  }

  List<String> get titles => handleStatus.isNullOrEmpty()
      ? ["服务完成", "继续服务", "拒单"]
      : ["服务完成", "继续服务", "拒单"];

  List<String> get sendTitles => handleStatus.isNullOrEmpty()
  ?['派单', '拒单']
  :['派单', '拒单'];

  var current = "";

  TabController? controller;

  BuildContext? pageContext;

  var handler = SliverOverlapAbsorberHandle();

  List<Widget> orderHandleWidget(BuildContext context) {
    if(detailData==null){
      return [];
    }
    switch (current) {
      case "待接单":
        return [
          SliverToBoxAdapter(
            child: ReceiveServiceOrderWidget(detailData!),
          )
        ];
      case "待服务":
        return [
          SliverToBoxAdapter(
            child: StartServiceOrderWidget(detailData!),
          )
        ];

      case "待开始":
        return [
          SliverToBoxAdapter(
            child: StartServiceOrderWidget(detailData!),
          )
        ];
      default:
        return [SliverToBoxAdapter()];
    }
  }

  List<Widget> _sliverHeader(BuildContext context, bool innerBoxIsScrolled) {

    return [
      SliverToBoxAdapter(
        child: ServiceOrderDetailInfoWidget(orderId, (detail) {
          detailData = detail;
          notifyListeners();
          switch (detail.statusId) {
            case "10231":
              if (current.isNullOrEmpty()) {
                current = "待派单";
                controller = TabController(
                    length: sendTitles.length, vsync: this.provider);
              }

              break;
            case "10233":
              if (current.isNullOrEmpty()) {
                current = "待接单";
              }
              break;
            case "10235":
            case "10242":
              if (current.isNullOrEmpty()) {
                current = "待服务";
              }
              break;
            case "10236":
              if (current.isNullOrEmpty()) {
                current = "待完成";
                controller =
                    TabController(length: titles.length, vsync: this.provider);
              }

              break;
          }
          print("111111===>${current}");
          notifyListeners();
        }),
      ),
      SliverToBoxAdapter(
        child: Container(
          height: 10,
        ),
      ),
      (!showDetail && current == "待完成" && controller != null)
          ? SliverPersistentHeader(
              floating: true,
              pinned: true,
              delegate: MySliverDelegate(
                  minHeight: 40,
                  maxHeight: 40,
                  child: Material(
                    child: TabBar(
                      tabs: titles
                          .map((e) => Tab(
                                text: e,
                              ))
                          .toList(),
                      indicator: BoxDecoration(),
                      labelColor: Colours.app_main,
                      unselectedLabelColor: Colors.grey,
                      controller: controller,
                    ),
                    color: Colors.white,
                  )))
          : (!showDetail && current == "待派单" && controller != null)
              ? SliverPersistentHeader(
                  floating: true,
                  pinned: true,
                  delegate: MySliverDelegate(
                      minHeight: 40,
                      maxHeight: 40,
                      child: Material(
                        child: TabBar(
                          tabs: sendTitles
                              .map((e) => Tab(
                                    text: e,
                                  ))
                              .toList(),
                          indicator: BoxDecoration(),
                          labelColor: Colours.app_main,
                          unselectedLabelColor: Colors.grey,
                          controller: controller,
                        ),
                        color: Colors.white,
                      )))
              : SliverToBoxAdapter()
    ];
  }

  Widget contentView(BuildContext context) {

    if (showDetail) {
      var items = _sliverHeader(context, true);
      items.add(SliverToBoxAdapter(
        child: ServiceOrderProcessWidget(orderId),
      ));
      return CustomScrollView(
        slivers: items,
      );
    } else {
      if (current == "待完成") {
        return NestedScrollView(
          headerSliverBuilder: _sliverHeader,
          body: controller == null||detailData==null
              ? Container()
              : TabBarView(
                  children: [
                    FinishServiceOrderWidget(detailData!),
                    ContinueServiceOrderWidget(),
                    RefuseOrderWidget(),
                  ],
                  controller: controller,
                ),
        );
      } else if (current == '待派单') {
        return controller == null
            ? Container()
            : NestedScrollView(
                headerSliverBuilder: _sliverHeader,
                body: TabBarView(
                  children: [
                    SendOrderFragment(),
                    OfficeRefuseOrderFragment(),
                  ],
                  controller: controller,
                ),
              );
      } else {
        var items = _sliverHeader(context, true);
        items.addAll(orderHandleWidget(context));
        return CustomScrollView(
          slivers: items,
        );
      }
    }
  }

  @override
  void onCreated() {
    super.onCreated();
  }

  @override
  void dispose() {
    detailData = null;
    super.dispose();
  }
}
